home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3801 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.4 KB  |  41 lines

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: how to pass matrix to function turbo c++?
  5. Date: 26 Jan 1996 02:21:41 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4e9drl$p87@colossus.holonet.net>
  8. References: <4e7ftm$11n@newsbf02.news.aol.com>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. OTDance (otdance@aol.com) wrote:
  13. : I make the matrix - no problem in main. . .
  14.  
  15. : I look for ways to pas the matrix to a function. . .big problems
  16. ...
  17.  
  18. From your code, I'll assume you have an 8x5 array of char.  If
  19. not, you can always adjust my answer to fit your case.  Here's
  20. the simple answer:
  21.  
  22. void func(char array[][5]) {    // leave the first [] empty
  23.    cout << array[2][3];         // ...or whatever
  24. }
  25. int main() {
  26.    char array[8][5] = { "abcd", "efgh", "ijkl" /* etc... */ };
  27.    func(array);                   // prints ^ this
  28. }
  29.  
  30. Note that a 2-d array in C (or C++) is simply an array of arrays.
  31. The rule for passing any array as argument is: it gets passed via
  32. a pointer to its first element.  In this case, the first element
  33. is a char[5] array, so yes, I could have written char (*array)[5] 
  34. instead of char array[][5], in the argument list.
  35.  
  36. BTW, I did not try to make the above const correct, or to use
  37. const int parameters instead of literal numbers -- both good
  38. habits to get into.
  39. --
  40. Russell Blackadar,   russell@mdli.com
  41.